package com.b2c.oa.controller;

import com.b2c.oa.DataConfigObject;
import com.ims.entity.MediaAccountEntity;
import com.ims.service.MediaAccountService;
import com.ims.service.MediaDataReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping(value = "/media")
public class MediaDataReportController {
    @Autowired
    private MediaAccountService mediaAccountService;
    @Autowired
    private MediaDataReportService reportService;

    /**
     *
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "/data_report", method = RequestMethod.GET)
    public String dataReport(Model model, HttpServletRequest request) {

        String page = request.getParameter("page");
        Integer pageIndex = 1;
        Integer pageSize = DataConfigObject.getInstance().getPageSize();
        Integer mediaAccountId = 0;
        if (!StringUtils.isEmpty(request.getParameter("pageSize"))) {
            try {
                pageSize = Integer.parseInt(request.getParameter("pageSize"));
            } catch (Exception e) {
            }
        }
        if (!StringUtils.isEmpty(request.getParameter("accountId"))) {
            try {
                mediaAccountId = Integer.parseInt(request.getParameter("accountId"));
                model.addAttribute("accountId",mediaAccountId);
            } catch (Exception e) {
            }
        }

        var result = reportService.getList(pageIndex, pageSize, mediaAccountId);
        model.addAttribute("pageIndex", pageIndex);
        model.addAttribute("pageSize", pageSize);
        model.addAttribute("totalSize", result.getTotalSize());
        model.addAttribute("lists", result.getList());

        //查询客户列表
        List<MediaAccountEntity> mediaList = mediaAccountService.getList(null);
        model.addAttribute("mediaList", mediaList);

        model.addAttribute("menuId", "media_data_report");
        return "media_data_report";

    }

    @RequestMapping(value = "/data_report_m", method = RequestMethod.GET)
    public String mDataReport(Model model, HttpServletRequest request) {

        String page = request.getParameter("page");
        Integer pageIndex = 1;
        Integer pageSize = DataConfigObject.getInstance().getPageSize();
        Integer mediaAccountId = 0;
        if (!StringUtils.isEmpty(request.getParameter("pageSize"))) {
            try {
                pageSize = Integer.parseInt(request.getParameter("pageSize"));
            } catch (Exception e) {
            }
        }
        if (!StringUtils.isEmpty(request.getParameter("accountId"))) {
            try {
                mediaAccountId = Integer.parseInt(request.getParameter("accountId"));
                model.addAttribute("accountId",mediaAccountId);
            } catch (Exception e) {
            }
        }

        var result = reportService.getList(pageIndex, pageSize, mediaAccountId);
        model.addAttribute("pageIndex", pageIndex);
        model.addAttribute("pageSize", pageSize);
        model.addAttribute("totalSize", result.getTotalSize());
        model.addAttribute("lists", result.getList());

        //查询客户列表
        List<MediaAccountEntity> mediaList = mediaAccountService.getList(null);
        model.addAttribute("mediaList", mediaList);

        model.addAttribute("menuId", "media_data_report");
        return "media_data_report_m";

    }

    @RequestMapping(value = "/data_report_detail_m", method = RequestMethod.GET)
    public String detail(Model model, @RequestParam Integer id, HttpServletRequest request) {
        var report = reportService.getById(id);
        model.addAttribute("report", report);

        model.addAttribute("menuId", "media_data_report");
        return "media_data_report_detail_m";

    }
}
